package com.fj.redisson.config;

import com.fj.redisson.aspect.ConcurrencySafeConfiguration;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;

/**
 * @author fj
 * @date 2023/12/27 16:26
 */
@EnableAspectJAutoProxy
@EnableConfigurationProperties({RedissonProperties.class})
@ConditionalOnClass(RedissonProperties.class)
@AutoConfiguration
@Import(ConcurrencySafeConfiguration.class)
public class RedissonAutoConfiguration {

    @ConditionalOnMissingBean(name = {"redissonClient"})
    @Bean(destroyMethod = "shutdown")
    public RedissonClient getRedissonClient(RedissonProperties properties){
        System.out.println(properties);
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://"+properties.getHost()+":"+properties.getPort())
                .setDatabase(properties.getDb())
                .setPassword(properties.getPassword())
                .setConnectionPoolSize(30)
                .setConnectionMinimumIdleSize(5);
        return Redisson.create(config);
    }
}
